package com.itheima.upload.web.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.util.Collection;

@WebServlet("/up.action")
@MultipartConfig
public class Upsload extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        //Part part = req.getPart("file");
        Collection<Part> parts = req.getParts();
        String name = req.getParameter("name");
        String path = getServletContext().getRealPath("/uploads/");
        File file = new File(path);

        if (!file.exists()){

            file.mkdirs();
        }
        for (Part pat :parts){
            String fn = pat.getSubmittedFileName();
            if(fn==null||fn.equals("")) continue;
                pat.write(path+fn);
            }


    }
}
